package com.imyours.webservice.services;

import java.util.Date;

import org.hibernate.Session;

import com.imyours.message.MsgCenter;
import com.imyours.model.message.MessageEntity;
import com.imyours.model.user.UserEntity;
import com.imyours.model.util.HibernateUtil;
import com.imyours.webservice.service.Context;
import com.imyours.webservice.service.ContextService;
import com.imyours.webservice.services.annotation.Service;

/**
 * 发送消息
 * 
 * @author SLevenc
 * 
 */
@Service(name = "sendMsg")
public class SendMsg extends ContextService {

	public SendMsg(Context context) {
		super(context);
	}

	public void execute() {
		UserEntity user = getUserEntity();
		if (user != null) {
			String body = context.getParameter().get("body");
			long reciver = Long.parseLong(context.getParameter().get("reciver"));
			Session se = HibernateUtil.getSessionFactory().openSession();
			UserEntity reciverUserEntity = (UserEntity) se.get(UserEntity.class, reciver);
			if (reciverUserEntity != null) {
				MessageEntity msg = new MessageEntity();
				msg.setContent(body);
				msg.setReciver(reciverUserEntity);
				msg.setSender(user);
				msg.setSendDate(new Date());

				MsgCenter.getInstance().sendMsg(msg);
				context.success();
			} else {
				context.fail(4, "no Data");
			}
		}

	}

}
